package com.onairm.baselibrary.mvp.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.onairm.baselibrary.mvp.base.MvpCallback;
import com.onairm.baselibrary.mvp.base.MvpPresenter;
import com.onairm.baselibrary.mvp.base.MvpView;

/**
 * Created by bqy on 2018/3/27.
 */

public abstract class MvpFragment<V extends MvpView, P extends MvpPresenter<V>> extends Fragment
        implements MvpView, MvpCallback<V, P> {

    private FragmentDelegate<V, P> fragmentDelegate;
    private P presenter;

    public FragmentDelegate<V, P> getFragmentDelegate() {
        if (this.fragmentDelegate == null) {
            this.fragmentDelegate = new FragmentMvpDelegateImpl<>(this);
        }
        return this.fragmentDelegate;
    }

    @Override
    public abstract P createPresenter();

    @Override
    public P getPresenter() {
        return presenter;
    }

    @Override
    public void setPresenter(P presenter) {
        this.presenter = presenter;
    }

    @Override
    public V getMvpView() {
        return (V) this;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        getFragmentDelegate().onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentDelegate().onCreate(savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getFragmentDelegate().onViewCreated(view, savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getFragmentDelegate().onCreateView(inflater, container, savedInstanceState);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getFragmentDelegate().onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        getFragmentDelegate().onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        getFragmentDelegate().onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        getFragmentDelegate().onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
        getFragmentDelegate().onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        getFragmentDelegate().onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getFragmentDelegate().onDestroy();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        getFragmentDelegate().onDetach();
    }
}
